import pandas as pd
from binance import Client

proxies = {
    'http': 'socks5://127.0.0.1:10010',
    'https': 'socks5://127.0.0.1:10010',
}
client = Client(requests_params={'proxies': proxies, 'timeout': 10})


def get_binance_data(limit_var, symbol, interval):
    """从Binance获取数据"""
    klines = client.futures_klines(
        symbol=symbol,
        interval=interval,
        limit=int(limit_var)
    )

    # klines = client.get_klines(symbol='BTCUSDT', interval='1h', limit=100)

    df = pd.DataFrame(klines, columns=[
        'Open time', 'open', 'high', 'low', 'close', 'volume',
        'Close time', 'Quote asset volume', 'Number of trades',
        'Taker buy base asset volume', 'Taker buy quote asset volume', 'Ignore'
    ])

    # df['Open time'] = pd.to_datetime(df['Open time'], unit='ms')
    df['Open time'] = pd.to_datetime(df['Open time'], unit='ms') + pd.Timedelta(hours=8)
    df.set_index('Open time', inplace=True)
    return df[['open', 'high', 'low', 'close', 'volume']].astype(float)

def get_all_futures_funding():
    all_funding_rates = client.futures_funding_rate()
    df = pd.DataFrame(all_funding_rates, columns=[
        'symbol', 'fundingTime', 'fundingRate', 'markPrice'
    ])
    return df

def get_all_futures_symbols():
    futures_exchange_info  = client.futures_exchange_info()
    print(futures_exchange_info)
    symbols = [symbol['symbol'] for symbol in futures_exchange_info ['symbols']]
    data = []
    for symbol in symbols:
        if symbol.endswith("USDT"):
            data.append(symbol)
    return data

# print(get_all_futures_symbols())
